package com.oscar.ehome.provider.gildeimage;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Util;
import com.oscar.ehome.R;


/**
 * Created by on 2017/5/19 14:37.
 * 开源图片框架Gilde工具类
 *
 * @author mengwanhe
 */

public class GildeUtils {

    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";


    /**
     * 是否禁止磁盘缓存加载图片
     *
     * @param url
     * @param context
     * @param imageView
     * @param type      缓存的类型
     *                  <li>磁盘缓存全部 DiskCacheStrategy.ALL</li>
     *                  <li>磁盘禁止缓存DiskCacheStrategy.NONE</li>
     */
    public static void loadImage(String url, Context context, ImageView imageView, DiskCacheStrategy type) {
        Glide.with(context).load(url).diskCacheStrategy(type).into(imageView);
    }

    /**
     * 是否禁止内存缓存加载图片
     *
     * @param url
     * @param context
     * @param imageView
     * @param skipMemoryCache 禁止内存缓存 true为禁止
     */
    public static void loadImage(String url, Context context, ImageView imageView, boolean skipMemoryCache) {
        Glide.with(context).load(url).skipMemoryCache(skipMemoryCache).into(imageView);
    }

    /**
     * 是否禁止内存/磁盘缓存加载图片
     *
     * @param url
     * @param context
     * @param imageView
     * @param type            缓存的类型
     *                        <li>磁盘缓存全部 DiskCacheStrategy.ALL</li>
     *                        <li>磁盘禁止缓存DiskCacheStrategy.NONE</li>
     * @param skipMemoryCache 禁止内存缓存 true为禁止
     */
    public static void loadImage(String url, Context context, ImageView imageView, DiskCacheStrategy type,
                                 boolean skipMemoryCache) {
        Glide.with(context).load(url).skipMemoryCache(skipMemoryCache).diskCacheStrategy(type).into(imageView);
    }

    /**
     * 清除内存中的缓存 必须在UI线程中调用
     *
     * @param context
     */
    public static void clearMemory(Context context) {
        Glide.get(context).clearMemory();
    }

    /**
     * 清除磁盘中的缓存 必须在后台线程中调用，建议同时clearMemory()
     *
     * @param context
     */
    public static void clearDiskCache(Context context) {
        Glide.get(context).clearDiskCache();
    }

    /**
     * 优先级加载图片
     *
     * @param url
     * @param context
     * @param imageView
     * @param priority  优先级  Priority.LOW/Priority.HIGH
     */
    public static void loadImageWithPriority(String url, Context context, ImageView imageView, Priority priority) {
        Glide.with(context).load(url).priority(priority).into(imageView);
    }

    /**
     * 加载gif图片
     *
     * @param gifUrl
     * @param context
     * @param imageViewGif
     */
    public static void loadImageGif(String gifUrl, Context context, ImageView imageViewGif) {
//        Glide
//                .with(context)
//                .load(gifUrl)
//                .asGif() //判断加载的url资源是否为gif格式的资源
//                .error(R.mipmap.ic_launcher)
//                .into(imageViewGif);
    }

    /**
     * 将资源ID转为Uri
     */
    public static Uri resourceIdToUri(int resourceId, Context context) {
        return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
    }

    /**
     * 加载网络图片
     *
     * @param url
     * @param imageView
     * @param context
     */
    public static void loadUrlImage(String url, ImageView imageView, Context context) {
        try {
            Glide.with(context).load(url).placeholder(R.color.font_black_6).error(R.color.font_black_6).crossFade()
                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(imageView);
        } catch (Exception mE) {
            return;
        }
    }

    /**
     * 加载网络图片默认头像
     *
     * @param url
     * @param imageView
     * @param context
     */
    public static void loadUrlImage3(String url, ImageView imageView, Context context) {
        try {
            Glide.with(context).load(url).placeholder(R.mipmap.moren).error(R.mipmap.moren).crossFade()
                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(imageView);
        } catch (Exception mE) {
            return;
        }
        ///////////////////////////////////////////////////////////skipMemoryCache(true)
    }

    /**
     * 加载网络图片默认头像
     *
     * @param url
     * @param imageView
     * @param context
     */
    public static void loadUrlImages(String url, ImageView imageView, Context context) {
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.moren).error(R.mipmap.moren).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(true).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
    }
    public static void loadUrlImagepdf(String url, ImageView imageView, Context context) {
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.pdf).error(R.mipmap.pdf).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(true).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
    }
    public static void loadUrlImageshipin(String url, ImageView imageView, Context context) {
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.shipin).error(R.mipmap.shipin).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(true).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
    }

    public static void loadUrlImageexcel(String url, ImageView imageView, Context context) {
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.excell).error(R.mipmap.excell).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(true).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
    }
    public static void loadUrlImageyinpin(String url, ImageView imageView, Context context) {
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.yinpin).error(R.mipmap.yinpin).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(true).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
    }
    public static void loadUrlImagewenjianyoushuju(String url, ImageView imageView, Context context) {
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.moren).error(R.mipmap.morenwenjianjia).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(true).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
    }
    public static void loadUrlImagewenjianwushuju(String url, ImageView imageView, Context context) {
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.moren).error(R.mipmap.morenwenjianjiawu).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).skipMemoryCache(true).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
    }
    /**
     * 用户头像
     * 加载网络图片默认头像
     *
     * @param url
     * @param imageView
     * @param context
     */
    public static void loadUrlImage4(String url, ImageView imageView, Context context) {
        if (url == null) {
            return;
        }
        if (imageView == null) {
            return;
        }
        if (Util.isOnMainThread()) {
            try {
                Glide.with(context).load(url).placeholder(R.mipmap.touxiang).error(R.mipmap.touxiang).crossFade()
                        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).into(imageView);
            } catch (Exception mE) {
                return;
            }
        }
        ///////////////////////////////////////////////////////////skipMemoryCache(true)
    }

    /**
     * 加载网络图片
     *
     * @param url
     * @param imageView
     * @param context
     */
    public static void loadUrlImage2(String url, ImageView imageView, Context context) {
        try {
            Glide.with(context).load(url)
                    // .placeholder(R.color.font_black_6)
                    // .error(R.color.font_black_6)
                    // .crossFade()
                    // .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true)
                    .into(imageView);
        } catch (Exception mE) {
            return;
        }
    }

    /**
     * 加载drawable图片
     *
     * @param resId
     * @param imageView
     * @param context
     */
    public static void loadResImage(int resId, ImageView imageView, Context context) {
        try {
            Glide.with(context).load(resourceIdToUri(resId, context)).placeholder(R.color.font_black_6).error(R.color.font_black_6)
                    .crossFade().into(imageView);
        } catch (Exception mE) {
            return;
        }
    }

    /**
     * 加载本地图片
     *
     * @param path
     * @param imageView
     * @param context
     */
    public static void loadLocalImage(String path, ImageView imageView, Context context) {
        try {
            Glide.with(context).load("file://" + path).placeholder(R.color.font_black_6).error(R.color.font_black_6)
                    .crossFade().into(imageView);
        } catch (Exception mE) {
            return;
        }
    }

    /**
     * 加载网络圆型图片
     *
     * @param url
     * @param imageView
     * @param context
     */
    public static void loadCircleImage(String url, ImageView imageView, Context context) {
        try {
            Glide.with(context).load(url).placeholder(R.mipmap.moren).error(R.mipmap.moren).crossFade()
                    .transform(new GlideCircleTransform(context)).into(imageView);
        } catch (Exception mE) {
            return;
        }
    }

    /**
     * 加载drawable圆型图片
     *
     * @param resId
     * @param imageView
     * @param context
     */
    public static void loadCircleResImage(int resId, ImageView imageView, Context context) {
        try {
            Glide.with(context).load(resourceIdToUri(resId, context)).placeholder(R.color.font_black_6).error(R.color.font_black_6)
                    .crossFade().transform(new GlideCircleTransform(context)).into(imageView);
        } catch (Exception mE) {
            return;
        }
    }

    /**
     * 加载本地圆型图片
     *
     * @param path
     * @param imageView
     * @param context
     */
    public static void loadCircleLocalImage(String path, ImageView imageView, Context context) {
        try {
            Glide.with(context).load("file://" + path).placeholder(R.color.font_black_6).error(R.color.font_black_6)
                    .crossFade().transform(new GlideCircleTransform(context)).into(imageView);
        } catch (Exception mE) {
            return;
        }
    }


    /**
     * 图片加载图片
     * net - 网络
     * net-n(normal) ：其他图片
     * net-p(portrait) ：头像
     * net-x(没有渐变动画)
     * net-b(静态图片)
     * nat - 本地
     */
//    public static void imgLoadPic(Context context, String urlOrPath, ImageView img, String type) {
//        if (Util.isOnMainThread()) {
//            if (type.contains("net")) {
//                if (type.contains("-p")) {//头像
//                /* 没有依赖这个库不用管 （没有用到） */
////                GifVolley.with(context).getImageLoader().load(urlOrPath).into(img);
//                    RequestOptions option = new RequestOptions();
//                    option.error(getGlideheadDrawable(context));
//                    option.placeholder(getGlideheadDrawable(context));
//                    RequestManager manager = Glide.with(context);
//                    manager.asBitmap();
//                    manager.load(urlOrPath)
//                            .thumbnail(0.1f)
//                            .apply(option)
//                            .into(img);
//                } else {
//                    if (type.contains("-x")) {
//                        Glide.with(context).load(urlOrPath).into(img);
//                    } else {
//                        RequestOptions options = new RequestOptions();
//                        options.error(getGlideDrawable(context));
//                        options.placeholder(getGlideDrawable(context));
//                        RequestManager manager = Glide.with(context);
////                    if (type.contains("-b")) {
//                        manager.asBitmap();
////                    }
//                        manager.load(urlOrPath)
//                                .thumbnail(0.1f)
//                                .apply(options)
//                                .into(img);
//                    }
//                }
//            } else if (type.contains("nat")) {
//                File file = new File(urlOrPath);
//                Glide.with(context)
//                        .load(file)
//                        .into(img);
//            }
//        }
//    }


    /**
     * 获取glide默认错误drawable
     */
    public static Drawable getGlideDrawable(Context context) {
        int defaultResId = R.mipmap.moren;
        return ContextCompat.getDrawable(context, defaultResId);
    }

    /**
     * 获取glide默认错误drawable
     */
    public static Drawable getGlideheadDrawable(Context context) {
        int defaultResId = R.mipmap.touxiang;
        return ContextCompat.getDrawable(context, defaultResId);
    }

}
